package org.oims.simpleserver.controllers.api.v1;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.oims.simpleserver.entities.db.User;
import org.oims.simpleserver.entities.results.SimpleResult;
import org.oims.simpleserver.services.UserService;
import org.oims.simpleserver.utils.ReturnResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping(value = {"/api/v1"})
public class AuthenticateController {
    private UserService userService;

    @PostMapping(value = {"/login"})
    public SimpleResult login(@RequestBody String params, HttpSession session, HttpServletResponse resp){
        try {
            JSONObject json = JSON.parseObject(params);
            String MpNo = json.getString("MpNo");
            String pwd = json.getString("pwd");
            User user = userService.login(MpNo, pwd);
            if(user != null){
                session.setAttribute("user", user);
                return ReturnResultUtil.getDataResult(user);
            }else {
                session.invalidate();
                return ReturnResultUtil.getErrorResult(1, "用户名或密码错误");
            }
        }catch (Exception e){
            e.printStackTrace();
            resp.setStatus(500);
            return ReturnResultUtil.getServerErrorResult();
        }
    }

    @GetMapping(value = {"/login"})
    public SimpleResult isLogin(HttpSession session, HttpServletResponse resp){
        try {
            User user = (User) session.getAttribute("user");
            if(user == null){
                return ReturnResultUtil.getErrorResult(1, "未登录");
            }else{
                return ReturnResultUtil.getDataResult(user);
            }
        }catch (Exception e){
            e.printStackTrace();
            resp.setStatus(500);
            return ReturnResultUtil.getServerErrorResult();
        }
    }

    @GetMapping(value = {"/logout"})
    public SimpleResult logout(HttpSession session, HttpServletResponse resp){
        try {
            Object user = session.getAttribute("user");
            if(user != null){
                session.invalidate();
                return ReturnResultUtil.getSuccessResult();
            }else {
                return ReturnResultUtil.getErrorResult(2, "不合法的请求");
            }
        }catch (Exception e){
            e.printStackTrace();
            resp.setStatus(500);
            return ReturnResultUtil.getServerErrorResult();
        }
    }

    @GetMapping(value = {"/ws-token"})
    public SimpleResult getWsToken(HttpSession session, HttpServletResponse resp){
        try {
            User user = (User) session.getAttribute("user");
            if(user != null){
                String token = userService.getToken(user.getMpNo());
                return ReturnResultUtil.getDataResult(token);
            }else {
                return ReturnResultUtil.getErrorResult(2, "不合法的请求");
            }
//            String token = userService.getToken("+86-17513289887");
//            return ReturnResultUtil.getDataResult(token);
        }catch (Exception e){
            e.printStackTrace();
            resp.setStatus(500);
            return ReturnResultUtil.getServerErrorResult();
        }
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

}
